package com.sx.rabbitmqtest.controller;

import com.alibaba.fastjson.JSON;
import com.sx.rabbitmqtest.bean.Order;
import com.sx.rabbitmqtest.bean.OrderStatus;
import com.sx.rabbitmqtest.mq.DelayMessageSender;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @author shengx
 * @date 2020/8/12 21:25
 */
@RestController
public class MyController {
    @Autowired
    private DelayMessageSender sender;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/order/{userId}/{productId}")
    public String order(@PathVariable("userId") String userId, @PathVariable("productId") String productId) {
        String id = UUID.randomUUID().toString().replace("-","");
        Order order = new Order();
        order.setUserId(userId);
        order.setOrderId(id);
        order.setOrderStatus(OrderStatus.PAYING);
        order.setOrderTime(String.valueOf(System.currentTimeMillis()));
        String str = JSON.toJSONString(order);
        System.out.println("tojson:" + str);
        sender.sendMsg(str, new CorrelationData(id));
        redisTemplate.opsForValue().set("order:" + userId + ":" + id, order);
        return "success";
    }

    @GetMapping("/pay/{userId}/{orderId}")
    public String pay(@PathVariable("userId") String userId, @PathVariable("orderId") String orderId) {
        Order order= (Order)redisTemplate.opsForValue().get("order:" + userId + ":" + orderId);
        if(order == null){
            return "fail, order is null";
        }
        if(order.getOrderStatus() == OrderStatus.PAYED){
            System.out.println("orderid:" + orderId + " is payed");
        }else if(order.getOrderStatus() == OrderStatus.TIMEOUT){
            System.out.println("orderid:" + orderId + " is timeout");
        }else {
            order.setOrderStatus(OrderStatus.PAYED);
            redisTemplate.opsForValue().set("order:" + userId + ":" + orderId, order);
        }
        return "success";
    }

    @GetMapping("/order/timeout/{userId}")
    public List<Order> timeoutOrder(@PathVariable("userId") String userId) {
        Set keys = redisTemplate.keys("order:" + userId + ":*");
        List<Order> orders = new ArrayList<>();
        keys.forEach( key -> {
            Order order = (Order)redisTemplate.opsForValue().get(key);
            if(order != null && order.getOrderStatus() == OrderStatus.TIMEOUT){
                System.out.println(order);
                orders.add(order);
            }
        });
        return orders;
    }

    @GetMapping("/order/all/{userId}")
    public List<Order> allOrder(@PathVariable("userId") String userId) {
        Set keys = redisTemplate.keys("order:" + userId + ":*");
        List<Order> orders = new ArrayList<>();
        keys.forEach( key -> {
            Order order = (Order)redisTemplate.opsForValue().get(key);
            System.out.println(order);
            orders.add(order);
        });
        return orders;
    }
}